package com.gzcc.controller;

import com.gzcc.model.Brand;
import com.gzcc.repository.BrandRepository;
import com.gzcc.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 描述: 品牌controller
 *
 * @Author: Mr.Zhong
 * @Date: 2018/6/13
 */
@Controller
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandRepository brandRepository;

    @Value("${ckeditor.path}")
    private String path;

    @Value("${imagefile.path}")
    private String imagepath;
    /**
     * 描述: 跳转到品牌列表页
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping()
    public ModelAndView brandList() {
        ModelAndView mv = new ModelAndView();
        List<Brand> brandList = brandRepository.findAll();
        mv.addObject("brandList", brandList);
        mv.setViewName("backend/brand/brand_list.btl");
        return mv;
    }

    /**
     * 描述: 跳转增加品牌页面
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("backend/brand/brand.btl");
        return mv;
    }

    /**
     * 描述: 增加品牌
     *
     * @param brand
     * @param title 标题
     * @param file  文件
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String detail(Brand brand, @RequestParam(value = "title") String title, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        UUID filename=UUID.randomUUID();
        try {
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File(path +  filename + ".jpg")));//保存图片到目录下
            out.write(file.getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String name = imagepath+filename + ".jpg";
        brand.setImage(name);
        brand.setTitle(title);
        Date date = new Date();
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String nowtime = simpleDateFormat.format(date);
        try {
            Date time = simpleDateFormat.parse(nowtime);
            brand.setCreatetime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        brandRepository.save(brand);

        return "redirect:/brand";


    }

    /**
     * 描述: 修改品牌
     *
     * @param
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/toEdit", method = RequestMethod.GET)
    public ModelAndView edit(@RequestParam("id") int id) {
        ModelAndView mv = new ModelAndView();
        Brand brand = brandRepository.findById(id).get();
        mv.addObject("brand", brand);
        mv.setViewName("backend/brand/toEditBrand.btl");
        return mv;
    }

    /**
     * 描述: 删除品牌
     *
     * @param
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam("id") long id) {
        brandRepository.deleteById((int) id);
        return "redirect:/brand";
    }

    /**
     * 描述: 查询品牌
     *
     * @param
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView seachbrand(@RequestParam("title") String title) {
        List<Brand> brandList = brandRepository.findAllByBrandNameLike(title);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("brandList", brandList);
        modelAndView.setViewName("backend/brand/brand_search_list.btl");
        return modelAndView;
    }


}
